package pers.wq.util;

import java.util.Iterator;
import java.util.Map;

public class TagManager {
	public static final int INPUT_TEXT = 0;
	public static final int INPUT_SELECT = 0;
	//����form String
	public static String formTag(String action,String method){
		String str = "<form name=\"form\" id=\"form\" method=\""+method+"\" accept-charset=\"utf-8\" action="
				+ "\""+action+"\">";
		return str;
	}
	/**
	 * @param $value 会写在 textarea 标签之间
	 * */
	public static String textareaTag(String name,String value){
		String str = "";
		if(!StringUtil.strIsNull(name)){
			str = "<textarea class=\"form-control\" id=\""+name+"\" name=\""+name+"\" value=\"\">"+value+"</textarea>";
		}
		return str;
	}
	
	public static String formTag(String dataListId,String controller,String method){
		String url = "cM_"+controller+"_"+dataListId+"_"+method+".action";
		return formTag(url,"post");
	}
	public static String input(String type,String name,String value){
		value = null == value ? "" : value;
		return "<input type=\""+type+"\" name=\""+name+"\" class=\"form-control\" value=\""+value+"\" />";
	}
	public static String input(String type,String name,String value,boolean disable){
		value = null == value ? "" : value;
		String reStr = "<input type=\""+type+"\" name=\""+name+"\" class=\"form-control\" value=\""+value+"\" ";
		if(disable){
			reStr += "disabled=\"disabled\" ";
		}
		reStr += "/>";
		System.out.println("TagManager" + reStr);
		return reStr;
	}
	public static String inputText(String name,String val){
		return input("text",name,val);
	}
	public static String inputTextDisable(String name,String val){
		return input("text",name,val,true);
	}
	public static String inputHidden(String name,String val){
		return input("hidden",name,val);
	}
	public static String selectTag(String name , String defaultKey , Map<String,String> mapElem){
		StringBuilder reStr = new StringBuilder("<select name=\""+name+"\">");
		System.out.println("TagManager 1");
		System.out.println("TagManager " + mapElem);
		if(StringUtil.strIsNull(defaultKey)){
			System.out.println("TagManager 1.2");
			reStr.append(optionTag("0","Please Select"));
			System.out.println("TagManager 2");
		}else{
			System.out.println("TagManager 1.3 ");
			System.out.println(mapElem.get(defaultKey));
			reStr.append(optionTag(defaultKey,mapElem.get(defaultKey)));
			System.out.println("TagManager 3");
		}
		System.out.println("TagManager 1.4");
		Iterator<String> iterator = mapElem.keySet().iterator();
		System.out.println("TagManager 4");
		while(iterator.hasNext()){
			String key = iterator.next();
			System.out.println("TagManager 5");
			reStr.append(optionTag(key,mapElem.get(key)));
			System.out.println("TagManager 6");
		}
		reStr.append("</select>");
		System.out.println(reStr);
		return reStr.toString();
	}
	public static String optionTag(String key ,String value){
		return "<option value=\""+key+"\">" +value+ "</option>";
	}
}
